// 个人获奖资料编辑页面

// 关闭VSCode非必要报错
// ignore_for_file: non_constant_identifier_names, override_on_non_overriding_member, annotate_overrides, unused_element, camel_case_types
// ignore_for_file: prefer_interpolation_to_compose_strings, must_be_immutable,

import 'package:flutter/material.dart';
import 'package:project_award_statistics/pages/mobile_material_design/login_page/login_page.dart';
import 'package:project_award_statistics/pages/mobile_material_design/themes/light_mode.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/add_award_info/dialog_addawardinfo.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/delete_award_info/dialog_delete_award_info.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/award_model.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/load_award_info.dart';
import 'package:project_award_statistics/utils/mobile_material_design/my_drawer/my_drawer.dart';

class ProfileEdit_Mobile extends StatefulWidget {
  @override
  String stu_name;
  ProfileEdit_Mobile({
    super.key,
    required this.stu_name,
  });
  
  State<ProfileEdit_Mobile> createState() => _ProfileEditState();
}

class _ProfileEditState extends State<ProfileEdit_Mobile> with TickerProviderStateMixin {

  List<award_model> user_award_list=List.empty();   // 初始化等下会从数据库中获取的学生信息
  late AnimationController _animationController_widget1;  //初始化动画总线1

  @override
  void initState ()
    {
      super.initState();   // 覆盖父类initState()初始化函数
      // String stu_name=widget.stu_name;   // 获取父类Widget里的学生姓名

      _animationController_widget1=AnimationController(   // 初始化
        duration: Duration(seconds:1),  // 设置动画持续时间
        vsync: this,
      );

      Future.delayed(Duration(seconds:0)).then((_){
        _animationController_widget1.forward();  // 动画总线1,启动!
      });   
      _loadAwardList();   
    }
  
  Future <void> _loadAwardList() async
    {
      List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);  
      setState(() {     // 更新获取用户数据的变量状态
        user_award_list=awards;
      });
    }

  Widget build(BuildContext context) {   
    @override
    String stu_name=widget.stu_name;
    
    // 设置关闭页面前释放动画动作
    void animation_dispose ()   
      {
        if (_animationController_widget1.isAnimating)  
          {
            _animationController_widget1.stop();
            _animationController_widget1.dispose();
          }   
      }

    @override
    void dispose ()   // 覆写析构函数
      {   
        animation_dispose();     
        super.dispose();    
      }   

    // 测试用:print(user_award_list);

    // 当右下角加号被触发(也就是要增加获奖信息)显示确认增加对话框 
    Future <void> ShowDialog_AddAwardProfile () async 
      {
        await showDialog(
          context: context, 
          builder: (context)
            {
              return Dialog_AddAwardInfo(
                stu_name: stu_name,
              );
            }
        );
        List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
        setState(() {   // 更新变量状态,及时进行列表刷新
          user_award_list=awards;
        });
      }

    // 按下删除键执行函数调出对话框询问用户是否需要进行删除操作
    Future <void> ShowDialog_DeleteAwardInfo (String award_name,String award_time) async 
      {
        await showDialog(
          context: context, 
          builder: (context){
            return Dialog_DeleteAwardInfo(
              stu_name: stu_name,
              award_time:award_time,
              award_name:award_name,
            );
          },
        );
        List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
        setState(() {   // 更新变量状态,及时进行列表刷新
          user_award_list=awards;
        });
      }

    return Scaffold(
      resizeToAvoidBottomInset: false,    // 防止安卓设备输入时导致程序显示面积被压缩
      appBar: AppBar(    // 设置标题栏
        elevation:0,     // 去掉标题栏与主体部分之间的阴影
        backgroundColor: Colors.transparent,
        foregroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text("个人获奖信息编辑"),
        actions: [    //设置右侧退出到主页的按钮
          IconButton(   
            onPressed:() {
              animation_dispose();  // 切换页面前先关闭动画
              Navigator.pushReplacement(  // 设置按钮跳转功能
                context, 
                MaterialPageRoute(  
                  builder: (context)=>PopScope(    // 切换页面后彻底销毁当前页面对象               
                    canPop: false,
                    child: LoginPage_Mobile(), 
                  )
                ),
              );
            },   // 设置按钮跳转功能
            icon: const Icon(Icons.exit_to_app),    // 设置图标
          ),
        ],
      ),
      drawer: MyDrawer(current_page: "/profile_edit_page",stu_name: stu_name,),
      floatingActionButton: Padding(
        padding: const EdgeInsets.only(bottom: 15.0,right: 15.0),
        child: FloatingActionButton(
          onPressed: ShowDialog_AddAwardProfile,
          backgroundColor: Colors.blueGrey,
          child: Icon(Icons.add),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left:15.0,right:15.0,top:20.0),
        child: Column(      // 设置列式布局
          children: [
            // 调试专用 Text("测试信息:列表长度是:${user_award_list.length}"),

            Row(
              children: [
                Text(
                  "这些是您在大学生活中获得的奖项:",
                  style: TextStyle(
                    fontSize: 20,
                  ),
                ),
              ],
            ),

            const SizedBox(height: 15,),
            
            user_award_list.isEmpty  // 判断获奖列表是否为空,如果为空就显示"你的获奖列表空空如也 ~"
            ? Expanded(
              child: Center(
                child: Text(
                  "你的获奖列表空空如也 ~",
                  style: TextStyle(
                    fontSize: 15,
                    color:lightMode.colorScheme.inversePrimary,
                  ),
                ),
              ),
            )
            : Flexible(   // 由于ListView.Builder要求高度无限,所以调用Flexible控件或者使用shrinkWrap:true防止Exception报错
              child: 
                SlideTransition(    // 使用并设置动画控件详情
                  position: Tween<Offset>(
                    begin: Offset(-1.5, 0),
                    end: Offset.zero,
                  ).animate(
                    CurvedAnimation(
                      parent: _animationController_widget1, 
                      curve: Curves.easeInOut,
                    ),
                  ),
                  child: 
                    ListView.builder(
                      itemCount: user_award_list.length,
                      itemBuilder: (context,index){
                        return Container(   // 设置单行容器布局设计
                          margin: EdgeInsets.symmetric(vertical: 8),
                          decoration: BoxDecoration(
                            color: Colors.grey.shade400,
                            borderRadius: BorderRadius.circular(12),
                          ),
                          child: Row(     // 布局每一行的设计
                            children: [
                              SizedBox(width: 20,),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(height: 6,),  // 设置上边界
                                    Text(
                                      "奖项名称: "+user_award_list[index].award_name,
                                      style: TextStyle(
                                        fontSize: 17,
                                      ),
                                    ),
                                    SizedBox(height: 6,),
                                    Text(
                                      "奖项地域等级: "+user_award_list[index].region_level,
                                      style: TextStyle(
                                        fontSize: 17,
                                      ),
                                    ),
                                    SizedBox(height: 6,),
                                    Text(
                                      "获奖等第: "+user_award_list[index].award_level,
                                      style: TextStyle(
                                        fontSize: 17,
                                      ),
                                    ),
                                    SizedBox(height: 6,),
                                    Text(
                                      "获奖时间: "+user_award_list[index].award_time+'年',
                                      style: TextStyle(
                                        fontSize: 17,
                                      ),
                                    ),
                                    SizedBox(height: 6,),  // 设置下边界
                                  ],
                                ),
                              ),
                              IconButton(   // 设置删除按钮
                                icon: Icon(Icons.delete),
                                onPressed:() async {
                                  await ShowDialog_DeleteAwardInfo(
                                    user_award_list[index].award_name, 
                                    user_award_list[index].award_time,
                                  );
                                },
                              ),
                              SizedBox(width: 5,),   //设置控件离末尾一定空间
                          ],
                        ),
                      );
                    },
                  ),
                ),
            ),   
          ],
        ),
      ),
    );
  }
}


/* 曾经"示范"的错误代码
for (i=0;i<user_award_list.length;i++) Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(   //方便设计行控件
                  decoration: BoxDecoration(
                    color: Colors.grey.shade400,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Row(     //设置与删除键平行的布局
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: 12,),
                          Row(
                            children: [
                              const SizedBox(width: 30,),  //设置与边距隔开
                              Text(
                                '奖项名称: '+user_award_list[i].award_name,
                                style: TextStyle(
                                  fontSize: 20,
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 10,),
                          Row(
                            children: [
                              const SizedBox(width: 30,),  //设置与边距隔开
                              Text(
                                '奖项等级: '+user_award_list[i].region_level,
                                style: TextStyle(
                                  fontSize: 20,
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 10,),
                          Row(
                            children: [
                              const SizedBox(width: 30,),  //设置与边距隔开
                              Text(
                                '获奖等第:'+user_award_list[i].award_level,
                                style: TextStyle(
                                  fontSize: 20,
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 12,),
                        ],
                      ),
                    ],
                  ),
                ),
                //TODO:删除按钮与删除方法应该写在这里
                SizedBox(height: 40,),
              ],
            ),
*/

/*草稿
ListView.builder(
                  itemCount: user_award_list.length,
                  itemBuilder: (context,index){
                    return Container(   //设置单行容器布局设计
                      margin: EdgeInsets.symmetric(vertical: 10),
                      decoration: BoxDecoration(
                        color: Colors.grey.shade400,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Row(     //布局每一行的设计
                        children: [
                          SizedBox(width: 20,),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: 10,),  //设置上边界
                                Text(
                                  "奖项名称: "+user_award_list[index].award_name,
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                                SizedBox(height: 10,),
                                Text(
                                  "奖项地域等级: "+user_award_list[index].region_level,
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                                SizedBox(height: 10,),
                                Text(
                                  "获奖等第: "+user_award_list[index].award_level,
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                                SizedBox(height: 10,),
                                Text(
                                  "获奖时间: "+user_award_list[index].award_time+'年',
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                                SizedBox(height: 10,),  //设置下边界
                              ],
                            ),
                          ),
                          IconButton(   //设置删除按钮
                            icon: Icon(Icons.delete),
                            onPressed:() async {
                              await ShowDialog_DeleteAwardInfo(
                                user_award_list[index].award_name, 
                                user_award_list[index].award_time,
                              );
                            },
                          ),
                          SizedBox(width: 15,),   //设置控件离末尾一定空间
                        ],
                      ),
                    );
                  },
                ),
*/